# -*- encoding: utf-8 -*-
# @File    : Control.py
# @Date    : 2020/12/25
# @Author  : 韩林新


"""
class
    CtrlId
    CtrlOp
    Rect
    Window
    Control
"""
import pyautogui
# from pywinauto import mouse
import time

from .config import log_filename
from .config import interval
from .config import act_log
from .config import debug

class CtrlId(object):
    """
    control id

    Atrributes\n
        type
        name
    """

    def __init__(self, ctype, cname):
        """
        Parameters\n
            ctype
            cname
        """
        self.type = ctype
        self.name = cname.replace('&','')

    def __str__(self):
        return "{},{}".format(self.type, self.name)


class CtrlOp(CtrlId):
    """
    control operation

    Attributes\n
        op
    """

    def __init__(self, cop, ctype, cname):
        CtrlId.__init__(self, ctype, cname)
        self.op = cop

    def __str__(self):
        return "{},{},{}".format(self.op, self.type, self.name)


class Rect(object):
    """
    矩形坐标

    Attributes:
        left,top,right,button
    """

    def __init__(self, left, top, right, bottom):
        self.left = left
        self.top = top
        self.right = right
        self.bottom = bottom

    def __str__(self):
        return "rect = ({},{},{},{})".format(self.left, self.top, self.right, self.bottom)


class Window(object):
    """窗口类

    Attributes:
        name: 窗口名称
        rect: 矩形坐标
    """

    def __init__(self, name, left, top, right, bottom):
        self.name = name.replace('&','')
        self.rect = Rect(left, top, right, bottom)

    def __str__(self):
        return "name = {}, {}".format(self.name, self.rect.__str__())

    def get_width(self):
        """
        获取窗口宽度
        """
        return self.rect.right - self.rect.left

    def get_height(self):
        """
        获取窗口高度
        """
        return self.rect.bottom - self.rect.top

    def get_center(self):
        """
        获取窗口中心坐标
        """
        x = (self.rect.left + self.rect.right)//2
        y = (self.rect.top + self.rect.bottom)//2
        return x, y


class Control(Window):
    """
    控件类

    Attributs：
        type: 控件类型
        Window 属性
    """

    def __init__(self, ctype, name, left, top, right, bottom):
        Window.__init__(self, name, left, top, right, bottom)
        self.type = ctype

    def toStr(self):
        return "{},{}".format(self.type, self.name)

    def __str__(self):
        return "control_type = {}, name = {}, {}".format(self.type, self.name, self.rect.__str__())

    def log(self):
        """
        记录控件
        """
        with open(log_filename, 'a+', encoding='utf-8') as f:
            # f.write(self.__str__()+"\n")
            f.write(self.toStr()+"\n")

    def logaction(self, action):
        """
        记录操作
        """
        with open(log_filename, 'a+', encoding='utf-8') as f:
            f.write(action + self.toStr()+"\n")
    
    def click(self):
        """
        点击控件
        """
        # mouse.move(coords=self.get_center())    # pywinauto
        pyautogui.moveTo(self.get_center()[
                         0], self.get_center()[1])    # pyautogui

        time.sleep(interval)
        # mouse.click(button='left', coords=self.get_center())    # pywinauto
        x = self.get_center()[0]
        y = self.get_center()[1]
        pyautogui.click(x,y)
        if debug:
            print("click ({},{}) , {}".format(x, y,self.toStr()))
        if act_log:
            self.logaction('CLICK,')

    def click_m(self):
        """
        点击控件，纵坐标修正+28
        """
        # mouse.move(coords=self.get_center())    # pywinauto
        x = self.get_center()[0]
        y = self.get_center()[1]+28
        pyautogui.moveTo(x,y)    # pyautogui

        time.sleep(interval)
        # mouse.click(button='left', coords=self.get_center())    # pywinauto

        pyautogui.click(x,y)
        if debug:
            print("click ({},{}) , {}".format(x, y,self.toStr()))
        if act_log:
            self.logaction('CLICK,')

    def move_to(self):
        """
        移动鼠标至控件
        """
        # mouse.move(coords=self.get_center())    # pywinauto

        x = self.get_center()[0]
        y = self.get_center()[1]

        if debug:
            print("move to ({},{})".format(x,y))

        pyautogui.moveTo(x,y)    # pyautogui

        # self.logaction('MOVE to ({},{})'.format(
        #     self.get_center()[0], self.get_center()[1]))

        if act_log:
            self.logaction('MOVE,')

    def update_move(self):
        """
        通过鼠标在控件上划过来更新控件信息
        """
        if debug:
            print("update_info")
        y = (self.rect.top + self.rect.bottom)//2
        pyautogui.moveTo(self.rect.left,y)
        pyautogui.moveTo(self.rect.right,y,duration=1)
        pyautogui.moveTo(self.rect.right,y+28)
        pyautogui.moveTo(self.rect.left,y+28,duration=1)

    def update_rect(self, left, top, right,bottom):
        self.rect.left = left
        self.rect.top = top
        self.rect.right = right
        self.rect.bottom = bottom



